<?php
/**
 * Created by PhpStorm.
 * User: DELL-PC
 * Date: 2018/9/5
 * Time: 15:42
 */

namespace Application\Common\Controller;


use J\Schema\ICurd;
use J\Schema\ICollection;
use J\Schema\Sprite;
use J\Exception;
use J\MVC\Controller;

class BaseController extends Controller
{
    /**
     * @var ICollection
     */
    protected $model;

    /**
     * @return ICollection
     * @throws Exception
     */
    protected function getModel()
    {
        if(!$this->model)
            $this->model = $this->getCollection($this->getName());

        return $this->model;
    }

    /**
     * @return ICurd
     * @throws Exception
     */
    protected function getCurd()
    {
        return $this->getModel()->getCurd();
    }

    //----------------------------------base option------------------------------------

    /**
     * 添加条目
     * @param array $add
     * @return int
     * @throws Exception
     */
    public function add($add = [])
    {
        $add = $this->beforeAdd($add);
        // 配置信息
        $add['status'] = 1;

        return $this->getCurd()->setElements($add)->add();
    }

    /**
     * 删除条目
     * @param bool $recycle
     * @param array|string $map
     * @return int
     * @throws Exception
     */
    public function delete($recycle = true, $map = [])
    {
        return $this->getCurd()->setRecycle($recycle)
            ->setWhere($this->prepareWhere($map))->delete();
    }

    /**
     * 修改条目
     * @param array|string $map 查询添加
     * @param $elements
     * @return int
     * @throws Exception
     */
    public function edit($map, $elements)
    {
        $elements = $this->beforeEdit($elements);

        return $this->getCurd()
            ->setElements($elements)
            ->setWhere($this->prepareWhere($map))
            ->edit();
    }

    /**
     * 批量恢复
     * @param $map
     * @return int
     * @throws Exception
     */
    public function recovery($map = [])
    {
        $map[] = new Sprite('status', 1, '<');
        return $this->getCurd()->setWhere($this->prepareWhere($map))->recycle();
    }

    /**
     * 批量删除
     *
     * @param array $map
     * @return mixed
     *
     * @throws Exception
     */
    public function clean($map = [])
    {
        $map[] = new Sprite('status', 1, '<');
        return $this->getCurd()->setWhere($this->prepareWhere($map))->clear();

    }

    /**
     * 回收站条目
     * @param $elements
     * @return array
     * @throws Exception
     */
    public function recycle($elements)
    {
        $where =  [new Sprite('status', 1, '<')];
        return array_values(
            $this->getCurd()
                ->setElements($elements)
                ->setWhere($this->prepareWhere($where))->select()
        );
    }

    /**
     * 修正新增动作
     * @param array $add
     * @return array
     */
    protected function beforeAdd($add = [])
    {
        return $add;
    }

    /**
     * 修正编辑动作
     *
     * @param array $elements
     * @return array
     */
    protected function beforeEdit($elements = [])
    {
        return $elements;
    }

    /**
     * 生成通用查询数组
     *
     * @param array $where
     * @return array
     */
    protected function prepareWhere($where = [])
    {
        return $where;
    }
}